/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "FullScreenPlugin.h"
REGISTER_PLUGIN_CLASS(FullScreenPlugin)

bool FullScreenPlugin::execute(const string&  action, cJSON* args, CallbackContext cbc)
{
    if(action == "isSupported") {
        PluginResult pluginResult(PluginResult::OK, true);
        cbc.sendPluginResult(pluginResult);
    }
    
    if(action == "isImmersiveModeSupported") {
        PluginResult pluginResult(PluginResult::OK, true);
        cbc.sendPluginResult(pluginResult);
    }
    
    if(action == "immersiveWidth") {
        m_cbc = cbc;
        executeArkTs("./FullScreen/FullScreenAction/FullScreenAction", 0, "", "AndroidFullScreen", cbc);
    }
    
    if(action == "immersiveHeight") {
        m_cbc = cbc;
        executeArkTs("./FullScreen/FullScreenAction/FullScreenAction", 1, "", "AndroidFullScreen", cbc);
    }
    
    if(action == "leanMode") {
        m_cbc = cbc;
        executeArkTs("./FullScreen/FullScreenAction/FullScreenAction", 2, "", "AndroidFullScreen", cbc);
    }
    
    if(action == "showSystemUI") {
        m_cbc = cbc;
        executeArkTs("./FullScreen/FullScreenAction/FullScreenAction", 3, "", "AndroidFullScreen", cbc);
    }
    
    if(action == "showUnderStatusBar") {
        m_cbc = cbc;
        executeArkTs("./FullScreen/FullScreenAction/FullScreenAction", 4, "", "AndroidFullScreen", cbc);
    }
    if(action == "showUnderSystemUI") {
        m_cbc = cbc;
        executeArkTs("./FullScreen/FullScreenAction/FullScreenAction", 5, "", "AndroidFullScreen", cbc);
    }
    
    if(action == "immersiveMode") {
        m_cbc = cbc;
        executeArkTs("./FullScreen/FullScreenAction/FullScreenAction", 6, "", "AndroidFullScreen", cbc);
    }
    
    if(action == "setSystemUiVisibility") {
        m_cbc = cbc;
        int nVisibility = 0;
        if(cJSON_GetArraySize(args) > 0) {
            nVisibility = cJSON_GetArrayItem(args, 0)->valueint;
        }
        if(nVisibility > 0) {
            char szBuf[32];
            sprintf(szBuf, "%d", nVisibility);
            executeArkTs("./FullScreen/FullScreenAction/FullScreenAction", 7, szBuf, "AndroidFullScreen", cbc);
        }
    }
    
    if(action == "resetScreen") {
        m_cbc = cbc;
        executeArkTs("./FullScreen/FullScreenAction/FullScreenAction", 8, "", "AndroidFullScreen", cbc);
    }
    
    if(action == "onArKTsResult") {
        return onArKTsResult(args);
    }
    
    return true;
}

bool FullScreenPlugin::onArKTsResult(cJSON* args)
{
    cJSON* content = cJSON_GetObjectItem(args, "content");
    string action = "";
    if(content != NULL) {
        action = content->valuestring;
    }
    
    if(action == "immersiveWidth" || action == "immersiveHeight") {
        cJSON* json = cJSON_GetObjectItem(args, "result");
        if(json != NULL && json->type == cJSON_Array) {
            int count = cJSON_GetArraySize(json);
            if(count > 0) {
                int nArg = atoi(cJSON_GetArrayItem(json, 0)->valuestring);
                m_cbc.success(nArg);
            }
        }
    } else {
        cJSON* json = cJSON_GetObjectItem(args, "result");
        if(json != NULL && json->type == cJSON_Array) {
            int count = cJSON_GetArraySize(json);
            if(count > 0) {
                string strSuccess = cJSON_GetArrayItem(json, 0)->valuestring;
                if(strSuccess == "success") {
                    m_cbc.success();
                } else {
                    m_cbc.error(strSuccess);
                }
            }
        }
    }
    return true;
}